<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <!-- 添加分类按钮 -->
      <el-button type="primary" class="butt" @click="addCateBut"
        >添加分类</el-button
      >
      <!-- 表格区域 -->
      <zk-table
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
      >
        <!-- 是否有效区域 -->
        <template slot="isok" slot-scope="scope">
          <i
            class="el-icon-error"
            v-if="scope.row.cat_deleted"
            style="color: red; font-size: 16px"
          ></i>
          <i
            class="el-icon-success"
            v-else
            style="color: lightgreen; font-size: 16px"
          ></i>
        </template>
        <!-- 排序区域 -->
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag
            size="mini"
            type="success"
            v-else-if="scope.row.cat_level === 1"
            >二级</el-tag
          >
          <el-tag size="mini" type="warning" v-else>三级</el-tag>
        </template>
        <!-- 操作区域 -->
        <template slot="opt" slot-scope="scope">
          <!-- 编辑按钮 -->
          <el-button
            size="small"
            type="primary"
            icon="el-icon-edit"
            circle
            @click="onEditBut(scope.row)"
          ></el-button>
          <!-- 删除按钮 -->
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            circle
            @click="onDeleteBut(scope.row.cat_id)"
          ></el-button>
        </template>
      </zk-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageListPata.pagenum"
        :page-sizes="[3, 5, 9]"
        :page-size="pageListPata.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 添加分类对话框 -->
      <el-dialog
        title="添加分类"
        :visible.sync="addCateDialog"
        width="50%"
        @close="addCateDialogClose"
      >
        <!-- 内容 -->
        <el-form
          ref="addCateDataRef"
          :model="addCateData"
          :rules="addCateDataRules"
          label-width="80px"
        >
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="addCateData.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级分类">
            <el-cascader
              v-model="selectkeys"
              :options="parateList"
              :props="{
                expandTrigger: 'hover',
                value: 'cat_id',
                label: 'cat_name',
                children: 'children',
                checkStrictly: true,
              }"
              @change="selectChange"
              clearable
              style="width: 100%"
            ></el-cascader>
          </el-form-item>
        </el-form>

        <!-- footer -->
        <span slot="footer" class="dialog-footer">
          <el-button @click="addCateDialog = false">取 消</el-button>
          <el-button type="primary" @click="addCateOn">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 编辑分类对话框 -->
      <el-dialog
        title="编辑分类"
        :visible.sync="editdialog"
        width="50%"
        @close="editdialogClose"
      >
        <el-form
          ref="editCateDataRef"
          :model="editCateData"
          :rules="addCateDataRules"
          label-width="80px"
        >
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="editCateData.cat_name"></el-input> </el-form-item
        ></el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="editdialog = false">取 消</el-button>
          <el-button type="primary" @click="EditButOk">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'Cate',

  data() {
    return {
      // 商品列表分页参数
      pageListPata: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      // 商品列表
      cateList: [],
      // 商品总数
      total: 0,
      // 定义columns
      columns: [
        {
          label: '分类',
          prop: 'cat_name',
        },
        // 定义为模板列
        {
          label: '是否有效',
          type: 'template',
          template: 'isok',
        },
        // 定义为排序
        {
          label: '排序',
          type: 'template',
          template: 'order',
        },
        // 操作
        {
          label: '操作',
          type: 'template',
          template: 'opt',
        },
      ],
      // 添加分类对话框
      addCateDialog: false,
      // 添加分类的数据绑定
      addCateData: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0,
      },
      // 添加分类input规则
      addCateDataRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
        ],
      },
      // 获取父级分类存放数组
      parateList: [],
      // 选中父级分类的id
      selectkeys: [],
      // 编辑分类
      editdialog: false,
      // 编辑分类数据
      editCateData: {
        id: 0,
        cat_name: '',
      },
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: this.pageListPata,
      })
      if (res.meta.status !== 200)
        return this.$message.error('获取商品列表失败')
      // 把数据赋值
      this.cateList = res.data.result
      // 总数赋值
      this.total = res.data.total
    },
    // 监听pagesize发生变化
    handleSizeChange(newSize) {
      this.pageListPata.pagesize = newSize
      this.getCateList()
    },
    // 监听pagenum发生变化
    handleCurrentChange(newNum) {
      this.pageListPata.pagenum = newNum
      this.getCateList()
    },
    // 展示添加分类对话框
    addCateBut() {
      this.getParateList()
      this.addCateDialog = true
    },
    // 获取父级分类的全部名称
    async getParateList() {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 },
      })
      if (res.meta.status !== 200)
        return this.$message.error('获取父级分类列表失败')
      this.parateList = res.data
      console.log(this.parateList)
    },
    selectChange() {
      console.log(this.selectkeys)
      if (this.selectkeys.length > 0) {
        this.addCateData.cat_pid = this.selectkeys[this.selectkeys.length - 1]
        this.addCateData.cat_level = this.selectkeys.length
      } else {
        this.addCateData.cat_pid = 0
        this.addCateData.cat_level = 0
      }
    },
    // 点击添加分类提交
    addCateOn() {
      this.$refs.addCateDataRef.validate(async (valid) => {
        if (!valid) return this.$message.error('请正确填写表单')
        const { data: res } = await this.$http.post(
          'categories',
          this.addCateData
        )
        if (res.meta.status !== 201) return this.$message.error('添加分类失败')
        this.$message.success('添加分类成功')
        this.getCateList()
        this.addCateDialog = false
      })
    },
    // 关闭添加分类对话框
    addCateDialogClose() {
      // 重置表单
      this.$refs.addCateDataRef.resetFields()
      // 清空选择器里的数据
      this.selectkeys = []
      // 清空数据
      this.addCateData.cat_pid = 0
      this.addCateData.cat_level = 0
    },
    // 删除按钮
    async onDeleteBut(id) {
      const { data: res } = await this.$http.delete('categories/' + id)
      if (res.meta.status !== 200) return this.$message.error('删除失败')
      this.$message.success('删除成功')
      this.getCateList()
    },
    // 编辑按钮打开对话框
    onEditBut(row) {
      this.editdialog = true
      this.editCateData.id = row.cat_id
      this.editCateData.cat_name = row.cat_name
    },
    // 编辑分类提交
    async EditButOk() {
      this.$refs.editCateDataRef.validate(async (valid) => {
        if (!valid) return this.$message.error('请正确填写表单')
        const { data: res } = await this.$http.put(
          'categories/' + this.editCateData.id,
          {
            cat_name: this.editCateData.cat_name,
          }
        )
        if (res.meta.status !== 200) return this.$message.error('编辑失败')
        this.$message.success('编辑成功')
        this.getCateList()
        this.editdialog = false
      })
    },
    //编辑框关闭重置
    editdialogClose() {
      this.$refs.editCateDataRef.resetFields()
    },
  },
}
</script>

<style lang="less" scoped>
.butt {
  margin: 10px 0;
}
</style>